package org.wattdepot.cli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.wattdepot.cli.processor.CarbonContentCommand;
import org.wattdepot.cli.processor.CommandLineInterface;

/**
 * Test for the CarbonContent command.
 * 
 * @author Kendyll Doi
 * @author Kimberly Heu
 *
 */
public class TestCarbonContentCommand extends CommandLineInterface {
  
  /**
   * Tests the CarbonContent methods. 
   * @throws Exception If problems occur. 
   */
  @Test
  public void testCarbonContentCommand() throws Exception {
    
    List<String> commandList = new ArrayList<String>(); // List of commands
    
    commandList.add("carboncontent");
    commandList.add("SIM_IPP");
    commandList.add("2009-11-15T12:00:00.000-10:00");
    commandList.add("sampling-interval");
    commandList.add("10");
    
    CarbonContentCommand command = new CarbonContentCommand(); // command
    
    // The result from executing the individual method
    String methodResult = 
      WattCarbonContent.wattCarbonContent("SIM_IPP", "2009-11-15T12:00:00.000-10:00", 10, client);
    // The result from executing corresponding command
    String commandResult = command.doCommand(commandList);
    
    // Ensure answer is correct
    assertTrue("Checking method result", methodResult.contains("2083"));
    // Ensure both results are correct
    assertEquals("Checking method and command results are equal", methodResult, commandResult);
  }
  
  /**
   * Test that the input to the command is validated.
   * 
   * @throws Exception if problem occurs
   */
  @Test
  public void testInputValidation() throws Exception {
    
    List<String> commandList = new ArrayList<String>();
    commandList.add("carboncontent");
    commandList.add("SIM_fake");
    commandList.add("2009-11-15T12:00:00.000-10:00");
    commandList.add("sampling-interval");
    commandList.add("10");
    
    CarbonContentCommand command = new CarbonContentCommand(); // command
    String commandResult = command.doCommand(commandList);
    
    // Test for an incorrect source
    //System.out.println(commandResult);
    assertTrue("Checking invalid source", commandResult.startsWith("Error"));
    
    commandList.remove(1);
    commandList.add(1, "SIM_IPP");
    commandList.remove(3);
    commandList.add(3, "interval");
    
    // Test for invalid use of "sampling-interval" argument
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid format for command", commandResult.contains("Invalid input"));

    // Test for 0 interval
    commandList.remove(3);
    commandList.add(3, "sampling-interval");
    commandList.remove(4);
    commandList.add(4, "0");
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid interval in commands", commandResult
        .contains("interval must be greater than 0"));

    commandList.remove(4);
    commandList.add(4, "10");    
    commandList.remove(2);
    commandList.add(2, "09-14-15");

    // Test for invalid use of "timestamp" argument
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid format of command", commandResult.contains("not correct"));
    
    // Test for too few arguments
    commandList.remove(4);
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid number of commands", commandResult.contains("Invalid input"));
  }
}
